<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

interface Product {
  id: number
  name: string
  price: number
  originalPrice: number
  description: string
  seller: string
  image: string
  condition: string
  category: string
}

const products = ref<Product[]>([])
const loading = ref(true)
const currentPage = ref(1)
const pageSize = ref(12)
const total = ref(0)
const selectedCategory = ref('all')

// 模拟数据
const mockProducts: Product[] = [
  {
    id: 1,
    name: "iPhone 13 Pro",
    price: 4500,
    originalPrice: 8000,
    description: "95新，无划痕，电池健康度90%",
    seller: "数码达人",
    image: "https://via.placeholder.com/200x200?text=iPhone",
    condition: "95新",
    category: "数码"
  },
  {
    id: 2,
    name: "MacBook Air M1",
    price: 6000,
    originalPrice: 10000,
    description: "8+256G，轻度使用，外观完美",
    seller: "学生党",
    image: "https://via.placeholder.com/200x200?text=MacBook",
    condition: "9成新",
    category: "数码"
  },
  {
    id: 3,
    name: "Nike Air Jordan",
    price: 800,
    originalPrice: 1200,
    description: "42码，穿过几次，鞋底轻微磨损",
    seller: "球鞋爱好者",
    image: "https://via.placeholder.com/200x200?text=Jordan",
    condition: "8成新",
    category: "服装"
  },
  {
    id: 4,
    name: "Kindle Paperwhite",
    price: 500,
    originalPrice: 900,
    description: "无背光版，保护得很好",
    seller: "读书人",
    image: "https://via.placeholder.com/200x200?text=Kindle",
    condition: "9成新",
    category: "数码"
  },
  {
    id: 5,
    name: "机械键盘",
    price: 200,
    originalPrice: 400,
    description: "Cherry轴，RGB背光，手感极佳",
    seller: "程序员",
    image: "https://via.placeholder.com/200x200?text=Keyboard",
    condition: "85新",
    category: "数码"
  },
  {
    id: 6,
    name: "自行车",
    price: 300,
    originalPrice: 800,
    description: "山地车，适合校园代步",
    seller: "毕业生",
    image: "https://via.placeholder.com/200x200?text=Bike",
    condition: "7成新",
    category: "交通"
  }
]

const categories = [
  { label: '全部', value: 'all' },
  { label: '数码', value: '数码' },
  { label: '服装', value: '服装' },
  { label: '交通', value: '交通' },
  { label: '书籍', value: '书籍' },
  { label: '其他', value: '其他' }
]

const fetchProducts = async () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    if (selectedCategory.value === 'all') {
      products.value = mockProducts
    } else {
      products.value = mockProducts.filter(p => p.category === selectedCategory.value)
    }
    total.value = products.value.length
    loading.value = false
  }, 500)
}

const handleCategoryChange = () => {
  currentPage.value = 1
  fetchProducts()
}

const handlePageChange = (page: number) => {
  currentPage.value = page
  fetchProducts()
}

const goToProductDetail = (id: number) => {
  router.push(`/market/${id}`)
}

const goToSell = () => {
  router.push('/market/sell')
}

onMounted(() => {
  fetchProducts()
})
</script>

<template>
  <div class="market">
    <!-- 页面标题和操作栏 -->
    <div class="page-header">
      <h1>跳蚤市场</h1>
      <el-button type="primary" @click="goToSell">
        <el-icon><Plus /></el-icon>
        发布商品
      </el-button>
    </div>

    <!-- 分类筛选 -->
    <el-card class="filter-card">
      <div class="filter-content">
        <div class="category-section">
          <span>商品分类：</span>
          <el-radio-group v-model="selectedCategory" @change="handleCategoryChange">
            <el-radio-button 
              v-for="category in categories" 
              :key="category.value" 
              :label="category.value"
            >
              {{ category.label }}
            </el-radio-button>
          </el-radio-group>
        </div>
      </div>
    </el-card>

    <!-- 商品列表 -->
    <div class="products-container">
      <el-row :gutter="20">
        <el-col 
          v-for="product in products" 
          :key="product.id" 
          :xs="24" 
          :sm="12" 
          :md="8" 
          :lg="6"
        >
          <el-card class="product-card" @click="goToProductDetail(product.id)">
            <div class="product-image">
              <img :src="product.image" :alt="product.name" />
              <div class="product-condition">{{ product.condition }}</div>
            </div>
            <div class="product-info">
              <h3 class="product-name">{{ product.name }}</h3>
              <p class="product-description">{{ product.description }}</p>
              <div class="product-price">
                <span class="current-price">¥{{ product.price }}</span>
                <span class="original-price">¥{{ product.originalPrice }}</span>
              </div>
              <div class="product-seller">
                <el-icon><User /></el-icon>
                {{ product.seller }}
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[12, 24, 48, 96]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handlePageChange(1)"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<style scoped>
.market {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  box-sizing: border-box;
  padding: 0 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h1 {
  margin: 0;
  color: #333;
  font-size: 2em;
}

.filter-card {
  margin-bottom: 20px;
}

.filter-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.category-section {
  display: flex;
  align-items: center;
  gap: 15px;
}

.category-section span {
  color: #666;
  font-weight: 500;
}

.products-container {
  margin-bottom: 30px;
}

.product-card {
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 20px;
  height: 100%;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
  border-radius: 8px;
  margin-bottom: 15px;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-condition {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.8em;
}

.product-info {
  padding: 0 10px;
}

.product-name {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 1.1em;
  font-weight: 600;
  line-height: 1.4;
}

.product-description {
  margin: 0 0 10px 0;
  color: #666;
  font-size: 0.9em;
  line-height: 1.4;
  height: 2.8em;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.current-price {
  color: #f56c6c;
  font-size: 1.2em;
  font-weight: bold;
}

.original-price {
  color: #999;
  font-size: 0.9em;
  text-decoration: line-through;
}

.product-seller {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #666;
  font-size: 0.9em;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}
</style>